﻿using UnityEngine;
using UnityEngine.UI;

namespace UnityView
{
    public class UILabel : UILayout
    {
        public readonly Text TextComponent;

        public string Text
        {
            set => TextComponent.text = value;
            get => TextComponent.text;
        }

        public Color TextColor
        {
            set { TextComponent.color = value; }
            get { return TextComponent.color; }
        }

        public Font Font
        {
            set { TextComponent.font = value; }
            get { return TextComponent.font; }
        }

        public int FontSize
        {
            set { TextComponent.fontSize = value; }
            get { return TextComponent.fontSize; }
        }

        public TextAnchor Alignment
        {
            set { TextComponent.alignment = value; }
            get { return TextComponent.alignment; }
        }

        protected Shadow shadowComponent;

        public Shadow ShadowComponent
        {
            get
            {
                if (shadowComponent == null)
                {
                    shadowComponent = UIObject.AddComponent<Shadow>();
                    shadowComponent.effectDistance = new Vector2(3, -3);
                }

                return shadowComponent;
            }
        }

        public Vector2 ShadowOffset
        {
            set { ShadowComponent.effectDistance = value; }
            get { return ShadowComponent.effectDistance; }
        }

        public bool ShadowEnable
        {
            set { ShadowComponent.enabled = value; }
            get { return ShadowComponent.enabled; }
        }

        public UILabel(string name = "UILabel") : this(UICanvas.GetInstance(), name)
        {
        }

        public UILabel(IViewLayout parent, string name = "Text View")
            : base(parent, name)
        {
            TextComponent = UIObject.AddComponent<Text>();
            TextComponent.alignment = TextAnchor.MiddleCenter;
            TextComponent.fontSize = UIConstant.DefaultFontSize;
            Font = UIConstant.DefaultFont;
        }

        public UILabel(GameObject gameObject) : base(gameObject)
        {
            TextComponent = gameObject.GetComponent<Text>() ?? gameObject.AddComponent<Text>();
            Font = UIConstant.DefaultFont;
        }

        public void FitSize()
        {
            Frame = new Frame(Frame.x, Frame.y, Frame.width, TextComponent.preferredHeight);
        }
    }

    public class TextView : UIView
    {
        public readonly UILabel Label;

        public string Text
        {
            set { Label.Text = value; }
            get { return Label.Text; }
        }

        public Color TextColor
        {
            set { Label.TextColor = value; }
            get { return Label.TextColor; }
        }

        public Font Font
        {
            set { Label.Font = value; }
            get { return Label.Font; }
        }

        public int FontSize
        {
            set { Label.FontSize = value; }
            get { return Label.FontSize; }
        }

        public TextAnchor TextAlignment
        {
            set { Label.Alignment = value; }
            get { return Label.Alignment; }
        }

        public Text TextComponent
        {
            get { return Label.TextComponent; }
        }

        public TextView(string name = "TextView") : this(UICanvas.GetInstance(), name)
        {
        }

        public TextView(IViewLayout parent, string name = "TextView")
            : base(parent, name)
        {
            Label = new UILabel(this);
            Label.FillParent();
        }

        public static GameObject BaseTextView(string name = "Text View")
        {
            var gameObject = BaseView(name);
            var text = gameObject.AddComponent<Text>();
            text.font = UIConstant.DefaultFont;
            text.fontSize = 32;
            text.alignment = TextAnchor.MiddleCenter;
            return gameObject;
        }
    }
}